package org.jimmy.tang.demo;

import java.util.Date;
import java.util.List;

import org.hibernate.Session;
import org.jimmy.tang.dto.Event;
import org.jimmy.tang.dto.Person;
import org.jimmy.tang.utils.HibernateUtil;

/**
 * @author jimmy
 * 
 */
public class EventManager {
    public static void main(String[] args) {
        EventManager mgr = new EventManager();

        mgr.createAndStoreEvent("My Event", new Date());

        List<Event> events = mgr.listEvents();
        for (int i = 0; i < events.size(); i++) {
            Event theEvent = events.get(i);

            System.out.println("Event: " + theEvent.getTitle() + " Time: "
                    + theEvent.getDate());
        }

        HibernateUtil.getSessionFactory().close();
    }

    /**
     * 插入
     * @param title
     * @param theDate
     */
    private void createAndStoreEvent(String title, Date theDate) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Event theEvent = new Event();
        theEvent.setTitle(title);
        theEvent.setDate(theDate);
        session.save(theEvent);
        session.getTransaction().commit();
    }

    /**
     * 查询
     * @return
     */
    private List<Event> listEvents() {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();

        List<Event> result = session.createQuery("from Event").list();
        session.getTransaction().commit();
        return result;
    }

    /**
     * 保存即修改
     * @param personId
     * @param eventId
     */
    private void addPersonToEvent(Long personId, Long eventId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Person aPerson = (Person) session.load(Person.class, personId);
        Event anEvent = (Event) session.load(Event.class, eventId);
        aPerson.getEvents().add(anEvent);
        session.getTransaction().commit();
    }

    /**
     * 脱管对象重新持久化
     * @param personId
     * @param eventId
     */
    private void addPersonToEvent1(Long personId, Long eventId) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Person aPerson = (Person) session
                .createQuery(
                        "select p from Person p left join fetch p.events where p.id = :pid")
                .setParameter("pid", personId).uniqueResult(); // Eager fetch
                                                               // the collection
                                                               // so we can use
                                                               // it detached
        Event anEvent = (Event) session.load(Event.class, eventId);
        session.getTransaction().commit();
        // End of first unit of work
        aPerson.getEvents().add(anEvent); // aPerson (and its collection) is
                                          // detached
        // Begin second unit of work
        Session session2 = HibernateUtil.getSessionFactory()
                .getCurrentSession();
        session2.beginTransaction();
        session2.update(aPerson); // Reattachment of aPerson
        session2.getTransaction().commit();
    }

    /**
     * 
     * @param personId
     * @param emailAddress
     */
    private void addEmailToPerson(Long personId, String emailAddress) {
        Session session = HibernateUtil.getSessionFactory().getCurrentSession();
        session.beginTransaction();
        Person aPerson = (Person) session.load(Person.class, personId);
        // adding to the emailAddress collection might trigger a lazy load of
        // the collection
        aPerson.getEmailAddresses().add(emailAddress);
        session.getTransaction().commit();
    }

}
